//
//  XTStatusPictureView.swift
//  MyBlog
//
//  Created by victor on 17/3/28.
//  Copyright © 2017年 victor. All rights reserved.
//

import UIKit

class XTStatusPictureView: UIView {

    var viewModel: XTStatusViewModel? {
        didSet {
            calcViewSize()
            
            // 设置urls
            urls = viewModel?.picURLs
        }
    }
    
    // 根据视图模型的配图视图大小，调整显示内容
    fileprivate func calcViewSize() {
        
        // 处理宽度
        // 1> 单图，根据配图视图的大小，修改 subviews[0] 的宽高
        if viewModel?.picURLs?.count == 1 {
            
            let viewSize = viewModel?.pictureViewSize ?? CGSize()
            
            // a) 获取第0个图像视图
            let view = subviews[0]
            view.frame = CGRect(x: 0,
                                y: XTStatusPictureViewOutterMargin,
                                width: viewSize.width,
                                height: viewSize.height - XTStatusPictureViewOutterMargin)
            
        } else {
            // 2> 多图(无图)，恢复 subview[0] 的宽高，保证九宫格布局的完整
            let view = subviews[0]
            view.frame = CGRect(x: 0,
                                y: XTStatusPictureViewOutterMargin,
                                width: XTStatusPictureItemWidth,
                                height: XTStatusPictureItemWidth)
        }
        
        // 修改高度约束
        self.snp.updateConstraints { (make) in
            make.height.equalTo(viewModel?.pictureViewSize.height ?? 0)
        }
    }
    
    /// 配图视图的数组
    fileprivate var urls: [XTStatusPictureModel]? {
        didSet {
            // 隐藏所有的imageView
            for imageView in subviews {
                imageView.isHidden = true
            }
            
            // 遍历urls 数组，顺序 设置头像
            var index = 0
            for url in urls ?? [] {
                
                // 获得对应索引的 imageView
                let imageView = subviews[index] as! UIImageView
                
                // 4张图像处理
                if index == 1 && urls?.count == 4 {
                    index += 1
                }
                
                // 设置图像
                imageView.xt_setImage(urlString: url.thumbnail_pic, placeholderImage: nil)
                
                // 显示图像
                imageView.isHidden = false
                
                index += 1
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension XTStatusPictureView {
    
    // 1. Cell 中所有的控件都是提前准备好
    // 2. 设置的时候，根据数据决定是否显示
    // 3. 不要动态创建控件(动态创建会影响性能)
    fileprivate func setupUI() {
        
        // 设置背景颜色
        backgroundColor = superview?.backgroundColor
        
        // 超出边界的内容不显示
        clipsToBounds = true
        
        // 循环创建9个imageView
        let count = 3
        let rect = CGRect(x: 0,
                          y: XTStatusPictureViewOutterMargin,
                          width: XTStatusPictureItemWidth,
                          height: XTStatusPictureItemWidth)
        
        for i in 0..<count * count {
            let imageView = UIImageView()
            
            // 按比例填充
            imageView.contentMode = .scaleAspectFill
            imageView.clipsToBounds = true
            
            // 行 -> y
            let row = CGFloat(i / count)
            
            // 列 - x
            let col = CGFloat(i % count)
            
            let offsetX = col * (XTStatusPictureItemWidth + XTStatusPictureViewInnerMargin)
            let offsetY = row * (XTStatusPictureItemWidth + XTStatusPictureViewInnerMargin)
            
            imageView.frame = rect.offsetBy(dx: offsetX, dy: offsetY)
            
            addSubview(imageView)
        }
    }
}
